package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.ScrollPictureDao;
import com.gusha.model.InfoContent;
import com.gusha.model.ScrollPicture;
import com.gusha.model.SysClassify;
import com.gusha.model.User;
import com.gusha.service.ScrollPictureService;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/scrollPicture")
public class ScrollPictureController {
	// 注入Dao
    @Autowired
    private ScrollPictureDao scrollPictureDao;
    
    // 注入Service
    @Autowired
    private ScrollPictureService scrollPictureService;

/*
    @CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/aaaa")
	@ResponseBody
	public JsonResult aaaa(String userName, String password, HttpSession session, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult(false);
		List<ScrollPicture> list=scrollPictureDao.findAll();
		jsonResult.setObj(list);
		jsonResult.setMsg("登录成功");
		jsonResult.setFlag(true);
		return jsonResult;
	}
 */
  
    // 查询列表
    @RequestMapping("list")
    @SysOperateLog(operate = "查看图片列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String name=WebValueUtil.getWebValue(request, "name");
        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        ScrollPicture picture=new ScrollPicture();
        if (StringUtils.isNotBlank(name)) {
           picture.setName(name);
        }
        
        
       // ScrollPicture scrollPicture = new ScrollPicture();
        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "addTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<ScrollPicture> page = scrollPictureService.findAll(pageable, picture);
        model.addAttribute("page", page);
        return "/admin/scrollPicture/list";
    }
    
    // 进入添加页面
    @RequestMapping("toAdd")
    public String toAdd(Model model) {
        return "/admin/scrollPicture/form";
    }
    
    	// 保存
        @RequestMapping("save")
        @ResponseBody
        @SysOperateLog(operate = "保存图片信息")
        public JsonResult save(ScrollPicture scrollPicture,HttpServletRequest request) {
            JsonResult result = new JsonResult(false);
            String pathOld = WebValueUtil.getWebValue(request, "pathOld");
            if(!pathOld.equals(scrollPicture.getPath())) {
            	 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                 MultipartFile file = multipartRequest.getFile("file");
                  // 要上传的目标文件存放路径
                 String newFileName = FileUtils.getFileName(file.getOriginalFilename());
                 String imagePath = Constants.getFilePath("info")+newFileName;
                 String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("info");
                 FileUtils.upload(file, localPath, newFileName);
                 scrollPicture.setPath(imagePath);
            }
            
            scrollPicture.setIsDelete(new BigInteger("0"));
            scrollPictureDao.saveAndFlush(scrollPicture);
            result.setFlag(true);
            result.setMsg("保存成功");
            return result;
        }    	

    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除图片信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (scrollPictureDao.existsById(id)) {
        	ScrollPicture scrollPicture = scrollPictureDao.getOne(id);
        	scrollPicture.setIsDelete(new BigInteger("1"));
        	scrollPictureDao.saveAndFlush(scrollPicture);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }
    
    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复资讯信息")
    public JsonResult recovery(BigInteger id) {
        JsonResult result = new JsonResult(false,"恢复失败");
        if (scrollPictureDao.existsById(id)) {
        	ScrollPicture infoContent = scrollPictureDao.getOne(id);
            infoContent.setIsDelete(new BigInteger("0"));
            scrollPictureDao.saveAndFlush(infoContent);
            result.setFlag(true);
            result.setMsg("恢复成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }
    
  //进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入图片编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(scrollPictureDao.existsById(new BigInteger(id))) {
            	ScrollPicture scrollPicture = scrollPictureDao.getOne(new BigInteger(id));
                model.addAttribute("obj", scrollPicture);
            }
        }        
        return "/admin/scrollPicture/form";
    }
    
}
